package com.danco.training.zaneuskaya.service.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.danco.training.zaneuskaya.domain.Job;
import com.danco.training.zaneuskaya.domain.User;
import com.danco.training.zaneuskaya.repository.JobDao;
import com.danco.training.zaneuskaya.repository.UserDao;
import com.danco.training.zaneuskaya.service.JobService;
/**
 * @author Zaneuskaya Aliaksandra
 *
 */
@Service("jobService")
@Transactional
public class JobServiceImpl implements JobService {

	private static final String JOB_NOT_FOUND = "Job not found";

	private static final Logger logger = Logger.getLogger(JobServiceImpl.class);

	@Autowired
	private JobDao jobDao;

	@Autowired
	private UserDao userDao;

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public JobDao getJobDao() {
		return jobDao;
	}

	public void setJobDao(JobDao jobDao) {
		this.jobDao = jobDao;
	}

	@Override
	public void createEntity(Job t) {
		try {

			jobDao.create(t);

		} catch (NullPointerException e) {
			logger.error(e.getMessage(), e);
		} catch (HibernateException e) {
			logger.error(e.getMessage(), e);
		}

	}

	@Override
	public void deleteEntityById(Integer id) {
		try {
			Job job = jobDao.read(id);
			jobDao.delete(job);
		} catch (NullPointerException e) {
			logger.error(e.getMessage(), e);
		} catch (HibernateException e) {
			logger.error(e.getMessage(), e);
		}
	}

	@Override
	public void updateEntity(Job t) {
		try {
			Job job = jobDao.read(t.getJobId());
			job.setCompanyTitle(t.getCompanyTitle());
			job.setJobCity(t.getJobCity());
			job.setJobCountry(t.getJobCountry());
			job.setJobEndDate(t.getJobEndDate());
			job.setJobStartDate(t.getJobStartDate());
			job.setPosition(t.getPosition());
			jobDao.update(job);

		} catch (NullPointerException e) {
			logger.error(e.getMessage(), e);
		} catch (HibernateException e) {
			logger.error(e.getMessage(), e);
		}
	}

	@Override
	public Job getEntityById(Integer id) {
		Job job = null;
		try {
			job = jobDao.read(id);
			if (job == null) {
				throw new NullPointerException(JOB_NOT_FOUND);
			}

		} catch (NullPointerException e) {
			logger.error(e.getMessage(), e);
		} catch (HibernateException e) {
			logger.error(e.getMessage(), e);
		}
		return job;
	}

	@Override
	public List<Job> getEntityList() {
		List<Job> jobs = null;
		try {
			jobs = jobDao.readAll();
		} catch (NullPointerException e) {
			logger.error(e.getMessage(), e);
		} catch (HibernateException e) {
			logger.error(e.getMessage(), e);
		}
		return jobs;
	}

	@Override
	public List<Job> getUserJobList(User user) {
		List<Job> jobs = null;
		try {
			user = userDao.read(user.getUserId());

			jobs = jobDao.findJobsByUser(user);

		} catch (NullPointerException e) {
			logger.error(e.getMessage(), e);
		} catch (HibernateException e) {
			logger.error(e.getMessage(), e);
		}
		return jobs;
	}

}
